package com.pushmsg.wappush;

public class WapPush
{
	WapPushMsgHdr wpmh;
	WapPushWspHdr wpwh;
	WapPushBody	  wpb;
	private byte[] data;
	private int segno=0;
	private byte[][] segment;
	private final int MAXMSGSIZE = 140;

	public WapPush(String siid, String msg, String url)
	{
		//System.out.println("siid="+siid);
		//System.out.println("msg="+msg);
		//System.out.println("url="+url);

		wpmh = new WapPushMsgHdr();
		wpb = new WapPushBody(siid, url, msg);
		wpwh = new WapPushWspHdr((byte)wpb.getLength());
	}

	public void setSiid(String val)
	{
		wpb.setSiid(val);
	}

	public void setMessage(String val)
	{
		wpb.setMessage(val);
	}

	public void setUrl(String val)
	{
		wpb.setUrl(val);
	}

	public byte[] getPushData()
	{
		byte[] temp;
		int ndx=0, i;
		data = new byte[wpmh.getLength()+wpwh.getLength()+wpb.getLength()];

		temp = wpmh.getData();
		for(i=0; i<wpmh.getLength(); i++)
		{
			data[ndx++ ] = temp[i];
		}

		temp = wpwh.getData();
		for(i=0; i<wpwh.getLength(); i++)
		{
			data[ndx++ ] = temp[i];
		}

		temp = wpb.getData();
		for(i=0; i<wpb.getLength(); i++)
		{
			data[ndx++ ] = temp[i];
		}

		return data;
	}
	public int getSegNum()
	{
		return segno;
	}

	public boolean process()
	{
		computeSegNo();
		segment = new byte[segno][];
		makeSegment();
		return true;
	}
	private void computeSegNo()
	{
		int MSGHDRLEN = 7; //the length of the message header(no segmentation)
		int bodylen = wpb.getLength();
		segno++;
		if(bodylen+MSGHDRLEN+wpwh.getLength()<=140) //if PUSH date has one segment
		{
			segno=1;
		}
		else
		{
			bodylen = bodylen - (140 - wpmh.getLength()-wpwh.getLength());
			//System.out.println("BODY LEN=" + bodylen);
			if(bodylen<0)
				segno--;
			while(bodylen>(140-wpmh.getLength()-wpwh.getLength()))
			{
				segno++;
				bodylen = bodylen - (140-wpmh.getLength()-wpwh.getLength());
			}
			segno++;
		}

	}

	private void makeSegment()
	{
		int segndx=0, ndx=0, maxmsgsize=MAXMSGSIZE;
		int bodystart=0, bodylen = wpb.getLength();
		byte[] bodydata = wpb.getData();
		byte[] buf = new byte[MAXMSGSIZE];
		wpmh.setTotalno(segno);

		if(segno==1)
		{
			segment[0]= new byte[wpmh.getLength()+wpwh.getLength()+wpb.getLength()];
			System.arraycopy(wpmh.getData(), 0, segment[0], 0, wpmh.getLength());
			System.arraycopy(wpwh.getData(), 0, segment[0], wpmh.getLength(), wpwh.getLength());
			System.arraycopy(wpb.getData(), 0, segment[0], wpmh.getLength()+wpwh.getLength(), wpb.getLength());
		}
		else
		{
			while(true)
			{
				wpmh.setCurno(segndx+1);
				for(int i=0; i<wpmh.getLength(); i++)
				{
					buf[ndx++]=wpmh.getData()[i];
					maxmsgsize--;
				}
				if(segndx==0)
				{
					for(int i=0; i<wpwh.getLength(); i++)
					{
						buf[ndx++]=wpwh.getData()[i];
						maxmsgsize--;
					}
				}
				for(int i=0; i<maxmsgsize&&i<bodylen; i++)
				{
					buf[ndx++]=bodydata[bodystart+i];
				}
				segment[segndx]=new byte[ndx];
				for(int i=0; i<ndx; i++)
					segment[segndx][i]=buf[i];
				bodylen -= maxmsgsize;
				bodystart += maxmsgsize;
				ndx=0;
				if(bodylen<=0)
					break;
				maxmsgsize=MAXMSGSIZE;
				segndx++;
			}
		}
	}

	public byte[] getSegment(int ndx)
	{
		if(ndx+1>segno)
			return null;
		else
			return segment[ndx];
	}
	public int  getHeadLength()
	{

			return wpmh.getLength();
	}
}